package org.leaf.green.b2b

class MarketPriceActualController {

    def marketPriceActualService

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : '/b2b/market/price/actual/list', model : [marketPriceActualInstanceList: marketPriceActualService.list(params), marketPriceActualInstanceTotal: marketPriceActualService.count()]
    }

    def create = {
        def marketPriceActualInstance = marketPriceActualService.init(params)
        //marketPriceActualInstance.properties = params
        render view : '/b2b/market/price/actual/create', model : [marketPriceActualInstance: marketPriceActualInstance]
    }

    def save = {
        def marketPriceActualInstance = new MarketPriceActual(params)
        if (!marketPriceActualInstance.hasErrors() && marketPriceActualInstance.save()) {
            flash.message = "marketPriceActual.created"
            flash.args = [marketPriceActualInstance.id]
            flash.defaultMessage = "MarketPriceActual ${marketPriceActualInstance.id} created"
            redirect(action: "show", id: marketPriceActualInstance.id)
        }
        else {
            render(view: "/b2b/market/price/actual/create", model: [marketPriceActualInstance: marketPriceActualInstance])
        }
    }

    def show = {
        def marketPriceActualInstance = MarketPriceActual.get(params.id)
        if (!marketPriceActualInstance) {
            flash.message = "marketPriceActual.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceActual not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/market/price/actual/show', model : [marketPriceActualInstance: marketPriceActualInstance]
        }
    }

    def edit = {
        def marketPriceActualInstance = MarketPriceActual.get(params.id)
        if (!marketPriceActualInstance) {
            flash.message = "marketPriceActual.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceActual not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/market/price/actual/edit', model : [marketPriceActualInstance: marketPriceActualInstance]
        }
    }

    def update = {
        def marketPriceActualInstance = MarketPriceActual.get(params.id)
        if (marketPriceActualInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (marketPriceActualInstance.version > version) {
                    
                    marketPriceActualInstance.errors.rejectValue("version", "marketPriceActual.optimistic.locking.failure", "Another user has updated this MarketPriceActual while you were editing")
                    render(view: "/b2b/market/price/actual/edit", model: [marketPriceActualInstance: marketPriceActualInstance])
                    return
                }
            }
            marketPriceActualInstance.properties = params
            if (!marketPriceActualInstance.hasErrors() && marketPriceActualInstance.save()) {
                flash.message = "marketPriceActual.updated"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceActual ${params.id} updated"
                redirect(action: "show", id: marketPriceActualInstance.id)
            }
            else {
                render(view: "/b2b/market/price/actual/edit", model: [marketPriceActualInstance: marketPriceActualInstance])
            }
        }
        else {
            flash.message = "marketPriceActual.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceActual not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def marketPriceActualInstance = MarketPriceActual.get(params.id)
        if (marketPriceActualInstance) {
            try {
                marketPriceActualInstance.delete()
                flash.message = "marketPriceActual.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceActual ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "marketPriceActual.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceActual ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "marketPriceActual.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceActual not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
